package utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import bean.ConnBean;

public class GetJobUtils {
	private static String jobinfo = "";
	private static final String sp = ",";
	private static final String lip = "'";

	
	public  ArrayList<ConnBean> getDbconfigList(Statement stmt,String dbConfigId) throws SQLException {
		ResultSet rs = stmt.executeQuery("select db_url,user_name,password,driver_name from DBconf where id in( "
				+ dbConfigId + ")");
		ArrayList<ConnBean> connBeans = new ArrayList<ConnBean>();
		while (rs.next()) {
			ConnBean connBean = new ConnBean();
			connBean.setUrl(rs.getString(1));
			connBean.setUsname(rs.getString(2));
			connBean.setPassword(rs.getString(3));
			connBean.setClassName(rs.getString(4));
			//connBean.setDbName(rs.getString(5));
			connBeans.add(connBean);
		}
		rs.close();
		return connBeans;
	}
	
	//查出该表下已有的job
	public  HashMap<String, String> getExistJob(String dbName,Statement stmt ) throws Exception{
		HashMap<String, String> hashMap = new HashMap<String, String>();
		ResultSet rs = stmt.executeQuery("SELECT job_name,job_id FROM "+jobinfo+" where src_db = '"+dbName+"'");
		while (rs.next()) {
			hashMap.put(rs.getString(1), rs.getString(2));
		}
		rs.close();
		return hashMap;
	}
	
		public   String getJobNameString(HashMap<String, String> hashMap) {
			StringBuffer buffer = new StringBuffer();
			int flg = 0;
			for (String key : hashMap.keySet()) {
				buffer.append(lip).append(key).append(lip);
				if (flg!=hashMap.size()-1) {
					buffer.append(sp);
				}
				++flg;
			}
			return buffer.toString();
		}
		
		public   String getJobNameString(ArrayList<String> hashMap) {
			StringBuffer buffer = new StringBuffer();
			int flg = 0;
			for (String key : hashMap) {
				buffer.append(lip).append(key).append(lip);
				if (flg!=hashMap.size()-1) {
					buffer.append(sp);
				}
				++flg;
			}
			return buffer.toString();
		}
		
}
